/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Linq;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif
namespace Oceanside
{
    class QuickCheckPayment : BindInterface
    {
        public QuickCheckPayment()
        {

        }

        override public System.Windows.Visibility IsVisible
        {
            get
            {
                try
                {
                    if (SHIFT.SelectedOrder != null && !SHIFT.SelectedOrder.CanCloseOrder &&
                        !SHIFT.SelectedOrder.IsClosed)
                    {
                        return System.Windows.Visibility.Visible;
                    }
                    return System.Windows.Visibility.Hidden;
                }
                catch (Exception e) { Logging.LogException(e); return System.Windows.Visibility.Hidden; }
            }
        }

        override public void Clicked()
        {
            try
            {
                AddCheckPaymentClicked();
            }
            catch (Exception ex)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_POBIND_CALL_CLOSECHECK, ex);
            }
        }

        private static void AddCheckPaymentClicked()
        {
            CloseCheckWindow.StopChangeDueBlink();

            decimal unpaid = SHIFT.SelectedOrder.CheckUnpaidGrandTotal(0);
            App.CashMoneyWindowInstance.ShowMe(unpaid, CashMoneyBox.CashBoxType.TYPE_PAYMENT);

            if (CashMoneyBox.LastResponse == UserResponse.ENTER)
            {
                if (CashMoneyBox.TotalAmount >= unpaid)
                {
                    TRANSACTION t = new TRANSACTION();
                    t.TerminalId = KIOSK.Get(SHIFT.GlobalContext).Id;
                    t.TransactionType = TRANSACTION.CONST_PAY_CHECK;
                    t.TenderType = TRANSACTION.CONST_TENDER_CHECK;
                    t.BusinessDayId = BUSINESS_DAY.GetNow(SHIFT.GlobalContext).Id;
                    t.Base = SHIFT.SelectedOrder.SelCheckUnpaidGrandTotal;

                    if (CashMoneyBox.TotalAmount > t.Base)
                    {
                        t.Change = CashMoneyBox.TotalAmount - t.Base;
                    }

                    t.OpenDateStamp = DateTime.Now;
                    t.CustomerName = SHIFT.SelectedOrder.CustomerName;
                    t.LastModifiedDateStamp = DateTime.Now;
                    t.ORDER = SHIFT.SelectedOrder;
                    t.PaidAtRevenueCenterId = KIOSK.Get(SHIFT.GlobalContext).RevenueCenterId;

                    t.OwningCheckNumber = 0;
                    t.UserId = SHIFT.Current.User.Id;
                    SHIFT.SelectedOrder.TRANSACTIONS.Add(t);
                    SHIFT.GlobalContext.SaveChanges();

                    if (KIOSK.HasLocalPrinter())
                    {
                        KIOSK k = KIOSK.Get(SHIFT.GlobalContext);

                        if (KIOSK.Get(SHIFT.GlobalContext).OpenDrawerOnPay && KIOSK.Get(SHIFT.GlobalContext).NumberOfCashDrawers != 0)
                        {
                            if (Constants.IsTrainingMode)
                            {
                                UMD.ShowGuiMessageSTASafe_NOTHROW("The cash drawer is disabled while in training mode to prevent theft.");
                            }
                            else
                            {
                                PopOwnersDrawer.Pop();
                            }
                        }

                        if (k.PrintCashReceiptOnPay)
                        {
                            for (int i = 0; i < k.NumberCashReceipts; i++)
                            {
                                PrintSpool.PrintCashReceipt((long)KIOSK.Get(SHIFT.GlobalContext).LocalPrinterId,
                                    SHIFT.SelectedOrder,
                                    t.Base,
                                    t.Change + t.Base,
                                    t.Id, 1);
                            }
                        }

                        if (k.PrintItemizedReceiptOnPay)
                        {
                            ORDER order = SHIFT.SelectedOrder;
                            PrintSpool.PrintItemizedReceipt(
                                (long)k.LocalPrinterId,
                                order,
                                order.SelCheckAutoGratutiy,
                                order.SelCheckRoundedSubtotal,
                                order.SelCheckTax, 0);
                        }
                    }
                    CloseTable.Message = "Order #" + SHIFT.SelectedOrder.TableName + "\nChange: " + t.Change.ToString("c");
                    CloseTable.Close(true);
                }
                else
                {
                    TRANSACTION t = new TRANSACTION();
                    t.TerminalId = KIOSK.Get(SHIFT.GlobalContext).Id;
                    t.CustomerName = SHIFT.SelectedOrder.CustomerName;
                    t.TransactionType = TRANSACTION.CONST_PAY_CHECK;
                    t.TenderType = TRANSACTION.CONST_TENDER_CHECK;
                    t.BusinessDayId = BUSINESS_DAY.GetNow(SHIFT.GlobalContext).Id;
                    t.Base = CashMoneyBox.TotalAmount;
                    t.Change = 0;

                    t.OpenDateStamp = DateTime.Now;
                    t.LastModifiedDateStamp = DateTime.Now;
                    t.ORDER = SHIFT.SelectedOrder;
                    t.PaidAtRevenueCenterId = KIOSK.Get(SHIFT.GlobalContext).RevenueCenterId;

                    t.OwningCheckNumber = 0;
                    t.UserId = SHIFT.Current.User.Id;
                    SHIFT.SelectedOrder.TRANSACTIONS.Add(t);
                    SHIFT.GlobalContext.SaveChanges();

                    if (KIOSK.HasLocalPrinter())
                    {
                        KIOSK k = KIOSK.Get(SHIFT.GlobalContext);
                        if (KIOSK.Get(SHIFT.GlobalContext).OpenDrawerOnPay && KIOSK.Get(SHIFT.GlobalContext).NumberOfCashDrawers != 0)
                        {
                            if (Constants.IsTrainingMode)
                            {
                                UMD.ShowGuiMessageSTASafe_NOTHROW("The cash drawer is disabled while in training mode to prevent theft.");
                            }
                            else
                            {
                                PopOwnersDrawer.Pop();
                            }
                        }

                        if (k.PrintCashReceiptOnPay)
                        {
                            for (int i = 0; i < k.NumberCashReceipts; i++)
                            {
                                PrintSpool.PrintCashReceipt((long)KIOSK.Get(SHIFT.GlobalContext).LocalPrinterId,
                                    SHIFT.SelectedOrder,
                                    t.Base,
                                    t.Change + t.Base,
                                    t.Id,1);
                            }
                        }

                        if (k.PrintItemizedReceiptOnPay)
                        {
                            ORDER order = SHIFT.SelectedOrder;
                            PrintSpool.PrintItemizedReceipt(
                                (long)k.LocalPrinterId,
                                order,
                                order.SelCheckAutoGratutiy,
                                order.SelCheckRoundedSubtotal,
                                order.SelCheckTax, 0);
                        }
                    }
                    OrderApi.Show();
                }
            }
        }
    }
}
